Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add and pass more regression tests for PerseusItem parser #1952

Merged
merged 22 commits into from
Jan 3, 2025

Conversation

benchristel
Copy link
Member

Issue: LEMS-2582

Test plan:

yarn test

@benchristel benchristel self-assigned this Dec 5, 2024
@benchristel
Copy link
Member Author

Note to reviewers: it's probably going to be easiest to read this one commit at a time.

Copy link
Collaborator

@jeremywiebe jeremywiebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is coming along nicely Ben! :)

One thing I noticed while reviewing. Do we have the concept of a tuple parser? I noticed some fields are definitely a [number, number] but we're parsing them as array(number).

Example from the matrix-widget.ts parser:

    matrixBoardSize: array(number),

Comment on lines +10 to +12
object({type: constant("ok")}),
object({type: constant("ok"), value: number}),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This confused me until I went and looked at the implementation and docs. I guess without having a "shape" that represents the discriminant, we'd have to rebuild how TypeScript decides what it is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree that this is ugly. For comparison, here's how Zod does it:

const myUnion = z.discriminatedUnion("status", [
  z.object({ status: z.literal("success"), data: z.string() }),
  z.object({ status: z.literal("failed"), error: z.instanceof(Error) }),
]);

I suppose we could do something like:

function discriminatedUnion<
  DiscriminantKey extends keyof any,
  T extends {[d: DiscriminantKey]: string | number | boolean}
>(discriminantKey: DiscriminantKey, parseVariant: Parser<T>): DiscriminatedUnionBuilder {
  // ...
}

That would mirror how TypeScript thinks about discriminated unions. But the problem is, it wouldn't handle our versioned widget options. Those aren't (in TS's view) valid discriminated unions, since the discriminant, version.major, is a nested field. My current parser design allows us to treat them kind of like discriminated unions anyway, by splitting the choice of variant and the parsing of that variant into separate steps that can run arbitrary parsing logic.

If only version were simply a number (like it should have been all along), we could just do the thing that makes intuitive sense here. The curse of pre-TS code strikes again! :/

...and now, after writing all that out, I am tempted to create a special parser abstraction just for versioned widget options. Then we could simplify the one for discriminated unions to be more like Zod's API.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like:

const parseMyWidget = parseVersionedWidgetOptions(parseVersion2)
  .migratedFrom(parseVersion1, upgradeFromVersion1)
  .migratedFrom(parseVersion0, upgradeFromVersion0)
  .parser

const input = {type: "ok", value: "foobar"};

expect(parse(input, unionParser)).toEqual(
failure(`At (root).value -- expected number, but got "foobar"`),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be useful to include the discriminant info here? Otherwise we don't know which union entry has bad data. 🤔

At (root {type: "ok"}).value -- expected number, but got "footer"

Maybe that gets too verbose.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that would be better. The parser context currently doesn't have a way to track which variant of a union you're currently trying to parse, so I can't think of a way to add this info to the message easily.

I'll try adding a forVariant method to ParseContext, parallel to forSubtree. Maybe that will be easier than I'm imagining.

const input = {type: "triangle", width: -1, radius: 99};

expect(parse(input, unionParser)).toEqual(
failure(`At (root).type -- expected "rectangle", but got "triangle"`)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice of this could mention all eligible types.

Comment on lines 41 to 44
// TODO(benchristel): simplify should never be `true`, but we
// have some content where it is anyway. If we ever backfill
// the data, we should simplify `simplify`.
simplify: optional(nullable(pipeParsers(union(string).or(constant(true)).parser).then(convert(String)).parser)),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -35,7 +35,10 @@ export const parseNumericInputWidget: Parser<NumericInputWidget> = parseWidget(
answers: array(
object({
message: string,
value: optional(number),
// TODO(benchristel): value should never be null or undefined,
// but we have some content where it is anyway. If we backfill
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benchristel benchristel force-pushed the benc/regression-tests-3 branch from 026af5c to c76c65f Compare January 2, 2025 17:31
Copy link
Contributor

github-actions bot commented Jan 2, 2025

npm Snapshot: Published

Good news!! We've packaged up the latest commit from this PR (efdc0ff) and published it to npm. You
can install it using the tag PR1952.

Example:

yarn add @khanacademy/perseus@PR1952

If you are working in Khan Academy's webapp, you can run:

./dev/tools/bump_perseus_version.sh -t PR1952

Copy link
Contributor

github-actions bot commented Jan 2, 2025

Size Change: +1.71 kB (+0.13%)

Total Size: 1.28 MB

Filename Size Change
packages/perseus/dist/es/index.js 419 kB +1.71 kB (+0.41%)
ℹ️ View Unchanged
Filename Size
packages/kas/dist/es/index.js 39 kB
packages/keypad-context/dist/es/index.js 760 B
packages/kmath/dist/es/index.js 4.27 kB
packages/math-input/dist/es/index.js 78 kB
packages/math-input/dist/es/strings.js 1.79 kB
packages/perseus-core/dist/es/index.js 1.48 kB
packages/perseus-editor/dist/es/index.js 688 kB
packages/perseus-linter/dist/es/index.js 22.2 kB
packages/perseus/dist/es/strings.js 4.93 kB
packages/pure-markdown/dist/es/index.js 3.67 kB
packages/simple-markdown/dist/es/index.js 12.5 kB

compressed-size-action

@benchristel benchristel merged commit 6173771 into main Jan 3, 2025
8 checks passed
SonicScrewdriver added a commit that referenced this pull request Jan 6, 2025
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @khanacademy/[email protected]

### Minor Changes

- [#1738](#1738)
[`dbbc82f2d`](dbbc82f)
Thanks [@anakaren-rojas](https://github.com/anakaren-rojas)! - add
scientific notation button / toggle to basic keypad

## @khanacademy/[email protected]

### Minor Changes

- [#1990](#1990)
[`37c642f24`](37c642f)
Thanks [@SonicScrewdriver](https://github.com/SonicScrewdriver)! - Allow
keyboards to navigate and interact with images


- [#1738](#1738)
[`dbbc82f2d`](dbbc82f)
Thanks [@anakaren-rojas](https://github.com/anakaren-rojas)! - add
scientific notation button / toggle to basic keypad

### Patch Changes

- [#2061](#2061)
[`d8b2f7eaf`](d8b2f7e)
Thanks [@anakaren-rojas](https://github.com/anakaren-rojas)! - update
terminology for angle sides


- [#2071](#2071)
[`bac10129b`](bac1012)
Thanks [@SonicScrewdriver](https://github.com/SonicScrewdriver)! - This
patch fixes our Perseus strings to ensure that they are double escaped
for Lingui.


- [#1952](#1952)
[`617377147`](6173771)
Thanks [@benchristel](https://github.com/benchristel)! - Internal: add
and pass more regression tests for PerseusItem parser


- [#2059](#2059)
[`53ba9f5d1`](53ba9f5)
Thanks [@mark-fitzgerald](https://github.com/mark-fitzgerald)! -
[Dropdown] Bugfix - Text in dropdown was shifted up after adding TeX
support via Renderer

- Updated dependencies
\[[`dbbc82f2d`](dbbc82f)]:
    -   @khanacademy/[email protected]

## @khanacademy/[email protected]

### Minor Changes

- [#1738](#1738)
[`dbbc82f2d`](dbbc82f)
Thanks [@anakaren-rojas](https://github.com/anakaren-rojas)! - add
scientific notation button / toggle to basic keypad

### Patch Changes

- Updated dependencies
\[[`d8b2f7eaf`](d8b2f7e),
[`bac10129b`](bac1012),
[`37c642f24`](37c642f),
[`617377147`](6173771),
[`dbbc82f2d`](dbbc82f),
[`53ba9f5d1`](53ba9f5)]:
    -   @khanacademy/[email protected]
    -   @khanacademy/[email protected]

## @khanacademy/[email protected]

### Patch Changes

- Updated dependencies
\[[`dbbc82f2d`](dbbc82f)]:
    -   @khanacademy/[email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants